//应用实例
const app = getApp()
Page({

  data: {
    courierArr: [],
    courier_list: null,
    courier_name: null,
    courier: [],
    courier_no: '',
    dict_id: '',
    order_number: '',
    index: 0,
    default: "请选择快递公司>",
    courierNum: '',
    address: '',
    name: '',
    phone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    this.setData({
      address: options.address,
      name: options.name,
      phone: options.phone_no
    })
    this.courierName()
  },
  // 快递公司
  courierName() {
    var that = this;
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'https://www.zbq888.cn/api/v1/dict/list',
      method: 'post',
      data: {
        code_type: "courier"
      },
      header: {
        'content-type': 'application/json', // 默认值
        "client": 1,
        'token': token
      },
      success(res) {
        // console.log(res.data)
        var courierList = [...res.data.dicts]; //放到一个数组里面
        var courierArr = res.data.dicts.map((item) => {
          return item.code_name
        }) //获取名称
        that.setData({
          courierArr: courierArr, //快递名称
          courier_list: courierList, //快递原数组列表
        })

      }
    })
  },
  // picker确认选择快递
  bindPickerChange: function (e) {
    let index = e.detail.value
    let courierArr = this.data.courierArr
    this.setData({
      index,
      default: courierArr[index],
    })
  },
  getcourierNum(e) {
    var val = e.detail.value;
    if (val.length >= 6) {
      if (/[0-9A-Za-z]{6,18}$/.test(val)) {
        this.setData({
          courierNum: val
        })
      } else {
        wx.showToast({
          title: '单号输入有误',
          duration: 600,
          icon: "none"
        })
      }
    }

  },
  // 快递发货
  faHuo() {
    var that = this;
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'https://www.zbq888.cn/api/v1/user/order/after/courier',
      method: 'post',
      data: {
        courier_no: that.data.courierNum,
        dict_id: that.data.dict_id,
        order_number: that.data.order_number
      },
      header: {
        'content-type': 'application/json', // 默认值
        "client": 1,
        'token': token
      },
      success(res) {
        // console.log(res)
        if (that.data.default == '请选择快递公司>') {
          wx.showToast({
            title: '请选择快递公司',
            icon: 'none'
          })
        } else if (that.data.courierNum == '' || '请输入快递单号') {
          wx.showToast({
            title: '请输入快递单号',
            icon: 'none'
          })
        } else if (res.data.mark == 0) {
          wx.showToast({
            title: '发货成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              url: '../cardDetail/cardDetail'
            })
          }, 1000)
        }
      }
    })
  },
  //  复制订单号码
  copyNum(e) {
    var datas = e.currentTarget.dataset.name + ' ' + e.currentTarget.dataset.phone +
      ' ' + e.currentTarget.dataset.text
    wx.setClipboardData({
      data: datas,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
})